import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import User from '../views/users/User.vue'
import Rights from '../views/rights/Rights.vue'
import Roles from '../views/rights/Roles.vue'
import Cate from '../views/goods/Cate.vue'
import Params from '../views/goods/Params.vue'
import Goods from '../views/goods/Goods.vue'
import Add from '../views/goods/Addgoods.vue'
import Order from '../views/Order.vue'
import Report from '../views/Report.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [{
      path: '/welcome', component: Welcome
    },
    { path: '/users', component: User },
    { path: '/rights', component: Rights },
    { path: '/roles', component: Roles },
    { path: '/categories', component: Cate },
    { path: '/params', component: Params },
    { path: '/goods', component: Goods },
    { path: '/goods/add', component: Add },
    { path: '/orders', component: Order },
    { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})
//  挂载路由导航守卫 全局路由守卫 每一个路由进入之前
router.beforeEach((to, from, next) => {
  //  to是当前路径
  //  from 上一级访问路径
  //  next 可调用该路由
  //  判断是否登录
  if (sessionStorage.getItem('token')) {
    //  登录：next()
    next()
  } else {
    //  未登录 1.判断是否登录
    if (to.path === '/login') {
      next()
    } else {
      //  2.不是登录页面 强制跳转到登录页面
      next('/login')
    }
  }
})
export default router
